home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992…ugust: Hack to the Future / ADC Developer CD (1992-08) (''Hack To The Future'')_iso / Dev.CD 199208.iso / Periodicals / develop / develop 9 code / Tracks / TestDrvr ƒ / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-06  |  4.9 KB  |  179 lines  |  [TEXT/MPS ]

  1. /**************************************************************
  2. Copyright 1990 Orion Network Systems, Inc. All Rights Reserved.
  3.  
  4. Name:        GetUserPrefs.c
  5.  
  6. Type:         Procedure Implementation
  7.  
  8. Function:    Returns handle to user prefs or nil if none.
  9.  
  10. Inputs:        System Folder ref. num from an environs call
  11.  
  12. Globals:    
  13.  
  14. Output:    
  15.  
  16. Side Effects:    
  17.  
  18.  
  19. **************************************************************/
  20. #include <Memory.h>
  21. #include <Resources.h>
  22. #include <Files.h>
  23. #include <Types.h>
  24. #include <Devices.h>
  25. #include <Events.h>
  26. #include <SysEqu.h>        /* FOR PORTABLE BUG */
  27. #include <Strings.h>
  28. #include <Errors.h>
  29. #include <Notification.h>
  30. #include <OSUtils.h>
  31.  
  32. #include "init.h"
  33.  
  34. pascal void InitMain()
  35. {
  36. short err;
  37. short refnum;
  38. StringPtr DrvrName;
  39. Handle dceHandle; /* FOR PORTABLE BUG */
  40. Handle drvrHandle; /* FOR PORTABLE BUG */
  41. Ptr entryPtr; /* FOR PORTABLE BUG */
  42. char myKeyMap[16];
  43. Boolean    shiftHeldDown;
  44. short iconID = 0;
  45.  
  46.     
  47.     /* Check if the shift key is down - if it is then DO NOT install: */
  48.     GetKeys((KeyMap) myKeyMap);
  49.     shiftHeldDown = myKeyMap[shiftKeyCode / 8] & (0x01 << (shiftKeyCode % 8));
  50.     
  51.     if (shiftHeldDown == false)
  52.     {
  53.     
  54.         /* Install and open the driver */
  55.         
  56.         DrvrName = "\p.TestDrvr";
  57.                 
  58.         err = InstallDriver(DrvrName);
  59.  
  60.         if (err == noErr)
  61.             err = OpenDriver(DrvrName, &refnum);    
  62.  
  63.         if (err == noErr)
  64.         {
  65.             entryPtr = *((Ptr *) UTableBase);
  66.             entryPtr += ((~refnum) * 4); 
  67.             dceHandle = *((Handle *) entryPtr);
  68.             drvrHandle = (Handle) ((DCtlEntry *) *dceHandle)->dCtlDriver;
  69.             if (drvrHandle != nil) /* Just in case */
  70.             HLock(drvrHandle);
  71.             iconID = 128;    // all went well...
  72.  
  73.         }
  74.     }
  75.     if (iconID != 0) 
  76.         ShowINIT(iconID, -1); /* Use moveX = -1 to move the default amount */
  77.         
  78.     return;
  79. }
  80.  
  81. /**********************************Comment*****************************************
  82. * changeDRVRSlot installs the driver into the slot passed in.  Because we want to keep
  83. * our DRVR resource ID in the resource file the same as it ever was, we GetResInfo on
  84. * it, and then set it back later.  But, before we set it back, we set the ID of the 
  85. * DRVR resource equal to the slot found, and then call OpenDriver.  OpenDriver uses the
  86. * resource ID of the DRVR resource to place it in the UnitTable -- pretty easy huh?
  87. * The only other tricky thing is we have to Detach the resource by calling DetachResource
  88. * so the resource doesn't go away when the resource file gets closed (the Resource
  89. * Manager is our friend).  The resource file gets closed when the INIT stops executing.
  90.  **********************************End Comment************************************/
  91. short changeDRVRSlot(short slot, StringPtr name)     /* Name is a pstring */
  92. {
  93. Handle            theDRVR;
  94. short            err, refNum;
  95. char            DRVRname[256];
  96. short            DRVRid;
  97. ResType            DRVRType;
  98.  
  99. if(slot != 0) 
  100.     {        
  101.         theDRVR = GetNamedResource('DRVR', name);
  102.         if (theDRVR)
  103.         {
  104.             GetResInfo(theDRVR, &DRVRid, &DRVRType, &DRVRname);
  105.             SetResInfo(theDRVR, slot, 0L);
  106.              
  107.             err = OpenDriver(name, &refNum);
  108.             if(err == noErr)
  109.                 {
  110.                 /* detach the resources from the resource map */
  111.                     DetachResource(theDRVR);
  112.          
  113.                 }
  114.             /* Restores the previous resource attributes so they don't change
  115.              * from startup to startup.  We just want the in-memory copy to have
  116.              * a different ID number -- not our resource in the file        */
  117.             theDRVR = GetNamedResource('DRVR', name);
  118.             SetResInfo(theDRVR, DRVRid, nil);
  119.         } else err = resNotFound;
  120.     }
  121.     return err;
  122. }
  123.  
  124. /**********************************Comment*****************************************
  125.  * InstallDriver installs a driver safely.  Taken from iacDriver example.
  126.  * it looks for a "slot" in the UnitTable into which the driver can be placed.  If
  127.  * it finds a "slot" it calls the procedure changeDRVRSlot to install the driver 
  128.  * into that slot.
  129.  **********************************End Comment************************************/
  130.  
  131. short InstallDriver(StringPtr drvrName)
  132. {
  133. short        tsSlot;
  134. short         err;
  135.  
  136.     if((tsSlot = lookForSlotInUnitTable()) != 0)
  137.         err = changeDRVRSlot(tsSlot, drvrName);
  138.     else err = unitTblFullErr;
  139.     
  140.     return err;
  141. }
  142.  
  143. /**********************************Comment*****************************************
  144.  * lookForSlotInUnitTable returns a short corresponding to a valid "slot" number.  It'll
  145.  range from 48 to UnitNTryCnt.  If there are no slots available it returns 0.  Essentially what
  146.  we're doing here is starting at the END of the UnitTable, testing the value at each
  147.  long-word location to see if it's nil.  If it's nil, that means we can place our driver
  148.  at that location, so we'll return the value of the "slot".
  149.  **********************************End Comment************************************/
  150. short 
  151. lookForSlotInUnitTable()
  152. {
  153. short        slot;
  154. Ptr        theBass;
  155. long        *theVoidPtr;
  156. Boolean     foundSlot;
  157.  
  158.     slot = *((short *)(UnitNtryCnt)) - 1;
  159.     theBass = (Ptr) (*((long *) (UTableBase)));
  160.     
  161.     foundSlot = false;
  162.     
  163.     while(slot>48 && !foundSlot) 
  164.         {
  165.         theVoidPtr = (long *)(theBass + (4L * slot));
  166.         
  167.         if(*theVoidPtr == 0L)
  168.             foundSlot = true;
  169.             
  170.         slot -= 1;
  171.         }
  172.         
  173.     slot += 1;
  174.     
  175.     if(!foundSlot)
  176.         slot = 0;
  177.  
  178.     return slot;
  179. }